﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BOM.entity;
using BOM.dao;

namespace BOM.BaseData
{
    public partial class MaterialFenLei : Form
    {
        public MaterialFenLei()
        {
            InitializeComponent();
        }
        //保存按钮
        private void btnBaoCun_Click(object sender, EventArgs e)
        {
            if (txtfname.Text.Trim() == "" || txtfnumber.Text.Trim() == "")
            {
                MessageBox.Show("编号或名称不能为空", "提示");
            }
            else
            {
                Materialgroup materialgroup = new Materialgroup();
                materialgroup.Fname = txtfname.Text;
                materialgroup.Fnumber = txtfnumber.Text;
                if(CustomerList.BiaoJi == 1)
                {
                    bool bol = MaterialgroupNew.UpdateIDMaterialgroup(MaterialList.JieDianID);
                    if (bol == true)
                    {
                        MaterialList matlist = (MaterialList)this.Owner;
                        MessageBox.Show("修改成功", "提示");
                        matlist.JieDianQingChu();
                        matlist.JiaZaiJieDian();
                    }
                    else
                    {
                        MessageBox.Show("修改失败", "提示");
                    }
                }
                else if(CustomerList.BiaoJi == 2)
                {
                    if (MaterialgroupNew.InsertMaterialgroup() == true)
                    {
                        MaterialList matlist = (MaterialList)this.Owner;
                        MessageBox.Show("新增成功", "提示");
                        matlist.JieDianQingChu();
                        matlist.JiaZaiJieDian();
                    }
                    else
                    {
                        MessageBox.Show("新增失败", "提示");
                    }
                }
            }
        }
        //窗体的加载事件
        private void MaterialFenLei_Load(object sender, EventArgs e)
        {
            try
            {
                if (CustomerList.BiaoJi != -1)
                {
                    DataTable table = MaterialgroupNew.QuryfidMaterialgroup(MaterialList.JieDianID);
                    if (table.Rows.Count == 1)
                    {
                        txtfnumber.Text = table.Rows[0]["fnumber"].ToString();
                        txtfname.Text = table.Rows[0]["fname"].ToString();
                    }
                }
                if (CustomerList.BiaoJi == 2) //新增
                {
                    txtfname.Text = "";
                    txtfnumber.Text = "";
                    btnShanChu.Enabled = false;
                    btnXiuGai.Enabled = false;
                }
                else if (CustomerList.BiaoJi == 1)//修改
                {

                }
            }
            catch
            { }
        }
        //新增按钮
        private void btnXinZeng_Click(object sender, EventArgs e)
        {
            txtfname.Text = "";
            txtfnumber.Text = "";
            btnXinZeng.Enabled = true;
            btnXiuGai.Enabled = false;
            CustomerList.BiaoJi = 2;
        }
        //修改按钮
        private void btnXiuGai_Click(object sender, EventArgs e)
        {
            btnBaoCun.Enabled = true;
            CustomerList.BiaoJi = 1;
        }
        //删除按钮
        private void btnShanChu_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("您确定要删除吗？", "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (dr == DialogResult.OK)
            {
                bool bol = MaterialgroupNew.DeleteFnumberMaterialgroup(txtfnumber.Text);
                if (bol == true)
                {
                    MaterialList matlist = (MaterialList)this.Owner;
                    MessageBox.Show("删除成功", "提示");
                    matlist.JieDianQingChu();
                    matlist.JiaZaiJieDian();
                }
                else
                {
                    MessageBox.Show("删除失败", "提示");
                }
            }
        }
        //退出按钮
        private void btnTuiChu_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
